package com.shy.base.vo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.google.gson.annotations.Expose;

public class PagerFilter {
    public static final String DIRECTION_DESC = "desc";
    public static final String DIRECTION_ASC = "asc";

    @Expose
    private Long max = 20L; // 最大条数
    private Long start = 0L; // 起始条数,开始条数下标
    @Expose
    private Long total; // 总条数

    @SuppressWarnings("rawtypes")
    @Expose
    private List objs = new ArrayList<>(); // 集合
    @Expose
    private Long pagePer = 1L; // 当前页数
    private String orderFilter; // 排序字段
    private String orderDirection; // 排序方向
    
    private String groupBy; //分組字段
    
    @Expose
    private Long totalpage; // 共页数
    private LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();

    private Long showPages; // 显示页标签数，请务必设置奇数，默认为7

    @Expose
    private Long showStart; // 输出开始页
    @Expose
    private Long showEnd; // 输出结束页

    /**
     * 获取当前显示起始頁，用於 < 5 6 7 8 9 >頁碼展現形式
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午6:57:14
     */
    public Long getShowStart() {
        Long maxMid = getShowPages() / 2 + 1;
        Long minMid = getShowPages() / 2;
        if (getTotalpage() <= getShowPages() || pagePer < maxMid) {
            return 1L;
        } else if (pagePer >= maxMid && pagePer < getTotalpage() - minMid) {
            return pagePer - minMid;
        } else {
            return getTotalpage() - getShowPages() + 1;
        }
    }

    public void setShowStart(Long showStart) {
        this.showStart = showStart;
    }

    /**
     * 結算显示結尾頁数
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:43:49
     */
    public Long getShowEnd() {
        Long maxMid = getShowPages() / 2 + 1;
        Long minMid = getShowPages() / 2;
        if (getTotalpage() <= getShowPages() || pagePer < maxMid) {
            return getTotalpage() >= getShowPages() ? getShowPages() : getTotalpage();
        } else if (pagePer >= maxMid && pagePer < getTotalpage() - minMid) {
            return pagePer + minMid;
        } else {
            return getTotalpage();
        }
    }

    public void setShowEnd(Long showEnd) {
        this.showEnd = showEnd;
    }

    public Long getShowPages() {
        return null == showPages || showPages == 0 ? 7 : showPages;
    }

    public void setShowPages(Long showPages) {
        this.showPages = showPages;
    }

    public PagerFilter() {
        super();
    }

    /**
     * 直接创建指定分页大小的分页对象
     * 
     * @param max
     */
    public PagerFilter(Long max) {
        super();
        this.max = max;
    }

    /**
     * 構造方法
     * 
     * @param pagePer
     * @param max
     * @param orderFilter
     * @param orderDirection
     */
    public PagerFilter(Long pagePer, Long max, String orderFilter, String orderDirection) {
        super();
        this.pagePer = pagePer;
        this.max = max;
        this.orderDirection = orderDirection;
        this.orderFilter = orderFilter;
        if (orderFilter != null && !orderFilter.equals("")) {
            orderby.put(orderFilter, orderDirection);
        }
    }

    /**
     * 添加排序
     * 
     * @param orderFilter
     * @param orderDirection
     */
    public void addOrderBy(String orderFilter, String orderDirection) {
        orderby.put(orderFilter, orderDirection);
    }

    /**
     * 計算当前頁碼
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:42:09
     */
    public long getCurrentPageNumber() { // 当前是第几页
        if (max == -1) {
            return 0;
        }
        if (start % max == 0) {
            return start / max + 1;
        } else {
            return start / max;
        }
    }

    /**
     * 計算總頁数
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:42:23
     */
    public long getPages() { // 总页数
        if (max == -1) {
            return 0;
        }
        if (total % max == 0) {
            return total / max;
        } else {
            return total / max + 1;
        }
    }

    public long getMax() {
        return max;
    }

    public void setMax(long max) {
        this.max = max;
    }

    /**
     * 获取起始條数
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:42:35
     */
    public long getStart() {
        if (this.pagePer >= 1) {
            long startCount = (this.pagePer - 1) * this.max;
            return startCount;
        } else {
            return start;
        }
    }

    /**
     * 获取總條数
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:42:48
     */
    public Long getTotal() {
        return total;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public void setTotalpage(long totalpage) {
        this.totalpage = totalpage;
    }

    /**
     * 获取總頁数
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:42:56
     */
    public long getTotalpage() {
        if (max == -1) {
            return 0;
        }
        if (total % max == 0) {
            return total / max;
        } else {
            return total / max + 1;
        }
    }

    /**
     * 获取結束條数
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:43:05
     */
    public long getEnd() {
        if (hasNext()) {
            return start + max;
        } else {
            if (max == -1) {
                return start + total;
            } else {
                return (start + (long) (total % max));
            }
        }
    }

    public long getNextStart() {
        return start + max;
    }

    public long getDisplayStart() {
        return start + 1;
    }

    public long getDisplayEnd() {
        return getEnd();
    }

    public long getPreviousStart() {
        return Math.max(0, start - max);
    }

    /**
     * 判斷是否有上一頁
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:43:20
     */
    public boolean hasPrevious() {
        return getCurrentPageNumber() > 1;
    }

    /**
     * 判斷是否有下一頁
     * 
     * @return
     * @author H2013788
     * @date 2017/11/22下午7:43:36
     */
    public boolean hasNext() {
        return getCurrentPageNumber() < getPages();
    }

    public long getLastStart() {
        return max * (this.getPages() - 1);
    }

    public String getOrderDirection() {
        return orderDirection;
    }

    public void setOrderDirection(String orderDirection) {
        this.orderDirection = orderDirection;
    }

    public String getOrderFilter() {
        return orderFilter;
    }

    public void setOrderFilter(String orderFilter) {
        this.orderFilter = orderFilter;
    }

    public Long getPagePer() {
        return pagePer;
    }

    public void setPagePer(long pagePer) {
        this.pagePer = pagePer;
    }

    @SuppressWarnings("rawtypes")
    public List getObjs() {
        return objs;
    }

    @SuppressWarnings("rawtypes")
    public void setObjs(List objs) {
        this.objs = objs;
    }

    public LinkedHashMap<String, String> getOrderby() {
        return orderby;
    }

    public void setOrderby(LinkedHashMap<String, String> orderby) {
        this.orderby = orderby;
    }

	public String getGroupBy() {
		return groupBy;
	}

	public void setGroupBy(String groupBy) {
		this.groupBy = groupBy;
	}

    
}
